using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ICP_MS.Plugin.AnalyteDb;

public class FormEquation : Form
{
	private List<InterferenceEquation> equations = new List<InterferenceEquation>();

	private AnalyteDB db = null;

	private AnalyteIsotope isotope = null;

	private string equation = "";

	private IContainer components = null;

	private Label label1;

	private Label lblEquation;

	private DataGridView dgvEquations;

	private Button btnOk;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;

	private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;

	private DataGridViewTextBoxColumn colSymbol;

	private DataGridViewTextBoxColumn colFactor;

	private Button btnCancel;

	public FormEquation(AnalyteDB db, AnalyteIsotope isotope)
	{
		this.db = db;
		this.isotope = isotope;
		InitializeComponent();
	}

	private void FormEquation_Load(object sender, EventArgs e)
	{
		ApplyResources();
		equation = isotope.equation;
		ShowEquations();
		GetEuations();
		dgvEquations.AutoGenerateColumns = false;
		dgvEquations.DataSource = equations;
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormEquation.Title.Text");
		label1.Text = Lang.Txt("FormEquation.Equation.Text");
		colFactor.HeaderText = Lang.Txt("FormEquation.Columns.Coeffient.Text");
		colSymbol.HeaderText = Lang.Txt("FormEquation.Columns.Element.Text");
		btnOk.Text = Lang.Txt("FormEquation.Button.OK.Text");
		btnCancel.Text = Lang.Txt("FormEquation.Button.Cancel.Text");
	}

	private void GetEuations()
	{
		IList<InterferenceEquation> equas = db.GetInterferenceEquation(isotope.id);
		if (equas != null && equas.Count > 0)
		{
			equations = new List<InterferenceEquation>(equas);
		}
		for (int i = equations.Count; i < 5; i++)
		{
			equations.Add(new InterferenceEquation());
		}
	}

	private void SaveEuations()
	{
		if (!db.DeleteInterferenceEuations(isotope.id))
		{
			return;
		}
		foreach (InterferenceEquation equa in equations)
		{
			if (equa.interferenceSymbol != null && equa.interferenceSymbol != "")
			{
				equa.id = 0;
				db.SaveInterferenceEquation(equa);
			}
		}
	}

	private void CalculateEquation()
	{
		equation = "";
		foreach (InterferenceEquation equa in equations)
		{
			if (equa.interferenceSymbol != null && equa.interferenceSymbol != "")
			{
				equation += $" {equa.interferenceFactor} * {equa.interferenceSymbol} ";
			}
		}
		ShowEquations();
	}

	private void ShowEquations()
	{
		lblEquation.Text = $"{isotope.symbol} = {isotope.intMass}M{equation}";
	}

	private void dgvEquations_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
	{
		int columnIndex = e.ColumnIndex;
		int num = columnIndex;
		if (num != 0 && num == 1)
		{
			double factor = Convert.ToDouble(e.Value);
			if (factor == 0.0)
			{
				e.Value = "";
			}
		}
	}

	private void dgvEquations_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
	{
		if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.ColumnIndex == colSymbol.Index)
		{
			FormMassSelect formMassSelect = new FormMassSelect(db, isotope.symbol);
			if (formMassSelect.ShowDialog() == DialogResult.OK)
			{
				equations[e.RowIndex].peakId = isotope.id;
				equations[e.RowIndex].interferenceId = formMassSelect.SelectedIsotope.id;
				equations[e.RowIndex].interferenceSymbol = formMassSelect.SelectedIsotope.symbol;
				equations[e.RowIndex].interferenceFactor = -1.0;
				dgvEquations.Refresh();
				CalculateEquation();
			}
		}
	}

	private void dgvEquations_KeyDown(object sender, KeyEventArgs e)
	{
		if (dgvEquations.SelectedRows.Count <= 0)
		{
			return;
		}
		foreach (DataGridViewRow row in dgvEquations.SelectedRows)
		{
			equations[row.Index].id = 0;
			equations[row.Index].interferenceSymbol = "";
			equations[row.Index].interferenceFactor = 0.0;
		}
		dgvEquations.Refresh();
		CalculateEquation();
	}

	private void btnOk_Click(object sender, EventArgs e)
	{
		isotope.equation = equation;
		SaveEuations();
		if (equation != null && equation != "")
		{
			isotope.withInterference = true;
		}
		base.DialogResult = DialogResult.OK;
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
	}

	private void dgvEquations_DataError(object sender, DataGridViewDataErrorEventArgs e)
	{
		MessageBox.Show("Input error.");
	}

	private void dgvEquations_CellValueChanged(object sender, DataGridViewCellEventArgs e)
	{
		if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
		{
			int columnIndex = e.ColumnIndex;
			int num = columnIndex;
			if (num != 0 && num == 1)
			{
				CalculateEquation();
			}
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
		this.label1 = new System.Windows.Forms.Label();
		this.lblEquation = new System.Windows.Forms.Label();
		this.dgvEquations = new System.Windows.Forms.DataGridView();
		this.colSymbol = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colFactor = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.btnOk = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
		((System.ComponentModel.ISupportInitialize)this.dgvEquations).BeginInit();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.BackColor = System.Drawing.SystemColors.Control;
		this.label1.Location = new System.Drawing.Point(26, 25);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(35, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "公式:";
		this.lblEquation.AutoSize = true;
		this.lblEquation.Location = new System.Drawing.Point(67, 25);
		this.lblEquation.Name = "lblEquation";
		this.lblEquation.Size = new System.Drawing.Size(71, 12);
		this.lblEquation.TabIndex = 1;
		this.lblEquation.Text = "M = 1.0 * M";
		this.dgvEquations.AllowUserToAddRows = false;
		this.dgvEquations.AllowUserToDeleteRows = false;
		dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dgvEquations.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
		this.dgvEquations.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dgvEquations.Columns.AddRange(this.colSymbol, this.colFactor);
		this.dgvEquations.Location = new System.Drawing.Point(12, 62);
		this.dgvEquations.Name = "dgvEquations";
		dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		this.dgvEquations.RowsDefaultCellStyle = dataGridViewCellStyle2;
		this.dgvEquations.RowTemplate.Height = 23;
		this.dgvEquations.Size = new System.Drawing.Size(247, 142);
		this.dgvEquations.TabIndex = 2;
		this.dgvEquations.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(dgvEquations_CellFormatting);
		this.dgvEquations.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dgvEquations_CellMouseDoubleClick);
		this.dgvEquations.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(dgvEquations_CellValueChanged);
		this.dgvEquations.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(dgvEquations_DataError);
		this.dgvEquations.KeyDown += new System.Windows.Forms.KeyEventHandler(dgvEquations_KeyDown);
		this.colSymbol.DataPropertyName = "interferenceSymbol";
		this.colSymbol.HeaderText = "元素";
		this.colSymbol.Name = "colSymbol";
		this.colSymbol.ReadOnly = true;
		this.colFactor.DataPropertyName = "interferenceFactor";
		this.colFactor.HeaderText = "系数";
		this.colFactor.Name = "colFactor";
		this.btnOk.Location = new System.Drawing.Point(276, 125);
		this.btnOk.Name = "btnOk";
		this.btnOk.Size = new System.Drawing.Size(75, 23);
		this.btnOk.TabIndex = 3;
		this.btnOk.Text = "确定";
		this.btnOk.UseVisualStyleBackColor = true;
		this.btnOk.Click += new System.EventHandler(btnOk_Click);
		this.btnCancel.Location = new System.Drawing.Point(276, 172);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(75, 23);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "取消";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.dataGridViewTextBoxColumn1.DataPropertyName = "interferenceSymbol";
		this.dataGridViewTextBoxColumn1.HeaderText = "元素";
		this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
		this.dataGridViewTextBoxColumn1.ReadOnly = true;
		this.dataGridViewTextBoxColumn2.DataPropertyName = "interferenceFactor";
		this.dataGridViewTextBoxColumn2.HeaderText = "系数";
		this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(375, 216);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOk);
		base.Controls.Add(this.dgvEquations);
		base.Controls.Add(this.lblEquation);
		base.Controls.Add(this.label1);
		base.Name = "FormEquation";
		base.ShowIcon = false;
		this.Text = "干扰校正公式编辑";
		base.Load += new System.EventHandler(FormEquation_Load);
		((System.ComponentModel.ISupportInitialize)this.dgvEquations).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
